import { BaseEvent } from "@/event/base";

export type CommandFunc = ()=>void

class KeyboardEventer extends BaseEvent {
  private _redo!:CommandFunc
  private _undo!:CommandFunc
  constructor(redo:CommandFunc,undo:CommandFunc){
    super()
    this._redo = redo
    this._undo = undo
  }

  addEventListen(): void {
    document.addEventListener('keydown',this._onKeydown.bind(this))
  }
  removeEventListen(): void {

  }

  _onKeydown(e: KeyboardEvent) {
    if(e.metaKey){
      if(e.key === 'z'){
        if(e.shiftKey){ //redo
          this._redo()
        }else{    // 撤销
          this._undo()
        }
      }
    }
  }
}

export default KeyboardEventer